<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        border
        stripe
        v-model:selections="selections"
      >
        <template #toolbar="{}">
          <el-button
            type="success"
            v-if="currentStatus == 0"
            size=""
            @click="openPay()"
          >
            批量已收款
          </el-button>
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button
              v-if="row.rebate_status == 0"
              type="primary"
              size="small"
              @click="openPay(row)"
            >
              收款
            </el-button>
            <el-button
              v-if="row.rebate_status == 200"
              type="primary"
              size="small"
              @click="openImages(row)"
            >
              凭证
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
      <!-- 收款 -->
      <Pay v-model="showPay" :data="current" @done="payDone" />
      <Images v-model="showImages" :data="current" />
    </ele-card>
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import Search from './components/search.vue';
  import Pay from './components/pay.vue';
  import Images from './components/images.vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { pageRebateFromInsuranceList } from '@/api/company';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        type: 'selection',
        columnKey: 'selection',
        width: 50,
        align: 'center',
        fixed: 'left',
        selectable: (row) => {
          // 禁用 id 为 45、47、48 的数据的复选框
          return row.id > 0;
        }
      },
      {
        prop: 'id',
        label: 'ID',
        width: 85,
        align: 'center'
      },
      {
        width: 110,
        prop: 'plate_number',
        label: '车牌号码'
      },
      {
        prop: 'company',
        label: '车队'
      },
      {
        width: 120,
        prop: 'insurance',
        label: '保险公司'
      },
      {
        width: 120,
        prop: 'insurance_salesman',
        label: '业务员'
      },
      {
        width: 80,
        prop: 'cate',
        label: '类型'
      },
      {
        width: 150,
        prop: 'price',
        label: '金额（不含车船税）'
      },
      {
        width: 165,
        prop: 'pay_time',
        label: '付款时间'
      },
      {
        width: 100,
        prop: 'rebate',
        label: '返点'
      },
      {
        width: 100,
        prop: 'rebate_amount',
        label: '返点金额'
      }
    );
    if (currentStatus.value == 200) {
      cols.push(
        {
          width: 160,
          label: '收款时间',
          prop: 'rebateTime'
        },
        {
          width: 100,
          label: '实收返点',
          prop: 'rebate_amount_paid'
        }
      );
    }

    cols.push({
      columnKey: 'action',
      label: '操作',
      slot: 'action',
      width: 80,
      align: 'center',
      fixed: 'right'
    });
    return cols;
  });

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);
  /** 表格数据源 */
  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageRebateFromInsuranceList({
      ...where,
      ...orders,
      page,
      limit
    });

    return result;
  };
  /** 还款操作 */
  const showPay = ref(false);
  const openPay = (row) => {
    const rows = row == null ? selections.value : [row];
    const isConsistent = rows.every(
      (item) => item.insurance === rows[0].insurance
    );
    if (!isConsistent) {
      EleMessage.warning('请选择类型一致的保险公司');
      return false;
    }
    current.value = rows;
    showPay.value = true;
  };

  const showImages = ref(false);
  const openImages = (row) => {
    console.log(row, '什么gi');
    current.value = row;
    showImages.value = true;
  };
  const payDone = () => {
    showPay.value = false;
    reload();
  };
  const currentStatus = ref(0);
  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
    currentStatus.value = where ? where.rebate_status : 0;
  };
</script>
